﻿@page "/Admin"
@using LuckyFish.Blog.Models
@using Microsoft.EntityFrameworkCore
@using Console = System.Console
@inject IDbContextFactory<BlogContext> DbFactory
@inject NavigationManager navigation
@inject DialogService DialogService

<PageTitle>管理员</PageTitle>

<Button OnClick="@(() => navigation.NavigateTo("/AddArticle"))">
    <i class="fa-solid fa-plus" aria-hidden="true"></i>添加文章
</Button>

<Row ColSpan="4">
    @foreach (var item in Array)
    {
        <Card class="item" IsShadow="true">
            <BodyTemplate>
                <Row RowType="RowType.Inline" ItemsPerRow="ItemsPerRow.Two">
                    <div>
                        <a href="@($"/Article/{item.UrlName}")" target="_blank">
                            <h5>@item.Title</h5>
                        </a>
                        <p class="card-text">@item.Brief</p>
                    </div>
                    <ImageViewer class="img" Url="@item.ImageUrl"/>
                </Row>
            </BodyTemplate>
            <FooterTemplate>
                <Row ItemsPerRow="ItemsPerRow.Six" ColSpan="4">
                    <Button IsAsync="true" OnClickWithoutRender="@(async () => await Change(item))">更改</Button>
                    <PopConfirmButton Placement="Placement.Top"
                                      Color="Color.Danger"
                                      ConfirmIcon="fa-solid fa-triangle-exclamation text-danger"
                                      ConfirmButtonColor="Color.Danger"
                                      Text="删除"
                                      IsAsync="true"
                                      OnConfirm="@(async () => await Remove(item))"
                                      Content="确定要删除数据吗？"/>
                </Row>
            </FooterTemplate>
        </Card>
    }
</Row>

@code {
    private List<ArticleModel> Array { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await using var context = await DbFactory.CreateDbContextAsync();
        Array = await context.Articles.ToListAsync();
    }

    private async Task Change(ArticleModel model)
    {
        var items = Utility.GenerateEditorItems<ArticleModel>();

        var option = new EditDialogOption<ArticleModel>()
        {
            Title = "edit dialog",
            Model = model,
            Items = items,
            ItemsPerRow = 2,
            RowType = RowType.Inline,
            OnCloseAsync = () =>
            {
                Console.WriteLine("close button is clicked");
                return Task.CompletedTask;
            },
            OnEditAsync = context =>
            {
                Console.WriteLine("save button is clicked");
                return Task.FromResult(true);
            }
        };

        await DialogService.ShowEditDialog(option);
    }

    public async Task Remove(ArticleModel model)
    {
        await using var context = await DbFactory.CreateDbContextAsync();
        context.Remove(model);
        await context.SaveChangesAsync();
        Array.Remove(model);
        StateHasChanged();
    }

}